/**
 * @author 邓勇治
 */

var _map = new Map();


/**
 * 字符串对象新功能，去除字符串两端的空格。
 */
String.prototype.trim = function () {
    var start, end;
    start = 0;
    end = this.length - 1;
    while (start <= end && this.charAt(start) == ' ') {
        start++;
    }
    while (start <= end && this.charAt(end) == " ") {
        end--;
    }
    return this.substring(start, end + 1);
}

/**
 * 字符串新功能，添加一个将字符串转成字符数组。
 * 返回一个数组
 */
String.prototype.toCharArray = function () {

    //定义一个数组。
    var chs = [];
    //将字符串中的每一位字符存储到字符数组中。
    for (var x = 0; x < this.length; x++) {
        chs[x] = this.charAt(x);
    }

    return chs;

}

/**
 * 字符串新功能，去除末尾数据
 * 返回一个数组
 */
String.prototype.delLastChar = function () {
    if (this == null || this == "") return this;
    return this.substring(0, this.length - 1);
}

/**
 * 添加一个，将字符串进行反转的方法。
 */

/*String.prototype.reverse = function () {
    var arr = this.toCharArray();

    //将数组位置置换功能进行封装。并定义到了反转功能内部。
    function swap(arr, a, b) {
        var temp = arr[a];
        arr[a] = arr[b];
        arr[b] = temp;
    }
}*/

/**
 * 生成随机数
 * @returns {string}
 */
String.prototype.getRandomId = function () {
    var d = new Date().getTime();
    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = (d + Math.random()*16)%16 | 0;
        d = Math.floor(d/16);
        return (c=='x' ? r : (r&0x3|0x8)).toString(16);
    });
    return this + uuid;
}

/**
 * Json字符串转Object
 * @returns {*}
 * {{result.item.toObject()}}
 * result.item.toJson();
 */
String.prototype.toObject = function () {
    try {
        if (JSON) {
            return JSON.parse(this);
        } else {
            return eval('(' + this + ')');
        }
    } catch (e) {
        console.error("Json转换失败！")
        return null;
    }
}

/*Object.prototype.toJsonString = function(){
    try {
        if (JSON) {
            return JSON.stringify(this)
        } else {
            return this.toLocaleString();
        }
    } catch (e) {
        console.error("Json转换失败！")
        return null;
    }
}*/

/**
 * 数组去重
 */
Array.prototype.unique = function () {
    this.sort();
    var re = [this[0]];
    for (var i = 1; i < this.length; i++) {
        if (this[i] !== re[re.length - 1]) {
            re.push(this[i]);
        }
    }
    return re;
}

/**
 * 查找指定元素的下标
 */
Array.prototype.exists = function (val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) return true;
    }
    return false;
};

/**
 * 查找指定元素的下标
 */
Array.prototype.indexOfFor = function (val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] == val) return i;
    }
    return -1;
};

/**
 * 删除指定元素
 */
Array.prototype.remove = function (val) {
    var index = this.indexOf(val);
    while (index > -1) {
        this.splice(index, 1);
        index = this.indexOfFor(val);
    }
    return this;
};
/**
 *
 */
Array.prototype.indexOfForObject = function(value) {
	for(var i = 0; i < this.length; i++) {
		if(JSON.stringify(this[i])  == JSON.stringify(value)) {
			return i;
		}
	}
	return -1;
};

Number.prototype.abs = function(){
    var num = (this || 0).toString(), result = '';
    while (num.length > 3) {
        result = ',' + num.slice(-3) + result;
        num = num.slice(0, num.length - 3);
    }
    if (num) { result = num + result; }
    return result;
}

function clone(obj) {
    // Handle the 3 simple types, and null or undefined
    if (null == obj || "object" != typeof obj) return obj;

    // Handle Date
    if (obj instanceof Date) {
        var copy = new Date();
        copy.setTime(obj.getTime());
        return copy;
    }

    // Handle Array
    if (obj instanceof Array) {
        var copy = [];
        for (var i = 0, len = obj.length; i < len; ++i) {
            copy[i] = clone(obj[i]);
        }
        return copy;
    }

    // Handle Object
    if (obj instanceof Object) {
        var copy = {};
        for (var attr in obj) {
            if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]);
        }
        return copy;
    }

    throw new Error("Unable to copy obj! Its type isn't supported.");
}



/**
*
*/
Array.prototype.existsForObject = function(value) {
	for(var i = 0; i < this.length; i++) {
		if(JSON.stringify(this[i])  == JSON.stringify(value)) {
			return true;
		}
	}
	return false;
}
/**
*
*/
Array.prototype.removeForObject = function(val) {
	var index = this.indexOfForObject(val);
	while(index > -1) {
		this.splice(index, 1);
		index = this.indexOfForObject(val);
	}
	return this;
}
Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1,
        "d+": this.getDate(),
        "h+": this.getHours(),
        "m+": this.getMinutes(),
        "s+": this.getSeconds(),
        "q+": Math.floor((this.getMonth() + 3) / 3),
        "S": this.getMilliseconds()
    }
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "")
            .substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
                : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}

//日期的转化
Date.prototype.format = function(format) {
    var o = {
        "M+" : this.getMonth() + 1,
        "d+" : this.getDate(),
        "h+" : this.getHours(),
        "m+" : this.getMinutes(),
        "s+" : this.getSeconds(),
        "q+" : Math.floor((this.getMonth() + 3) / 3),
        "S" : this.getMilliseconds()
    }
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "")
            .substr(4 - RegExp.$1.length));
    }
    for ( var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
                : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
}

Array.prototype.remove = function(val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};

function getFormatDate(date, pattern) {
    if (date == undefined) {
        date = new Date();
    }
    if (pattern == undefined) {
        pattern = "yyyy-MM-dd hh:mm:ss";
    }
    return date.format(pattern);
}

function formatTime(t) {
    if(t=='null' || t==null || t == undefined){
        return '';
    }
    if(t==0){
        return '';
    }
    var pattern = "yyyy-MM-dd hh:mm:ss";
    return getFormatDate(new Date(t), pattern);
}


function formatDate(t) {
    if(t=='null' || t==null || t == undefined){
        return '';
    }
    return getFormatDate(new Date(t), "yyyy-MM-dd");
}

function format(t,pattern) {
    if(t=='null' || t==null || t == undefined || pattern=='null' || pattern==null || pattern == undefined){
        return '';
    }
    if(t==0){
        return '';
    }
    return getFormatDate(new Date(t), pattern);
}
//yyyy-MM-dd mm:hh:ss  -->  时间戳
function changDate(str){
    if(str == '0'){
        return '0';
    }else{
        var date = new Date(str.replace(/-/g,'/'));
        return date.getTime();
    }

}

String.prototype.format = function(args) {
    var result = this;
    if (arguments.length > 0) {
        if (arguments.length == 1 && typeof (args) == "object") {
            for (var key in args) {
                if(args[key]!=undefined){
                    var reg = new RegExp("({" + key + "})", "g");
                    result = result.replace(reg, args[key]);
                }
            }
        }
        else {
            for (var i = 0; i < arguments.length; i++) {
                if (arguments[i] != undefined) {
                    //var reg = new RegExp("({[" + i + "]})", "g");//这个在索引大于9时会有问题，谢谢何以笙箫的指出

                    var reg= new RegExp("({)" + i + "(})", "g");
                    result = result.replace(reg, arguments[i]);
                }
            }
        }
    }
    return result;
}

Number.prototype.toDate = function () {
    return new Date(this);
}

Number.prototype.toDateFormat = function (format) {
    var date = new Date(this);
    return date.format(format);
}

Number.prototype.toyyyyMMdd = function () {
    var date = new Date(this);
    return date.format("yyyy-MM-dd");
}

Number.prototype.toyyyyMMddhhmmss = function () {
    var date = new Date(this);
    return date.format("yyyy-MM-dd hh:mm:ss");
}

String.prototype.toyyyyMMdd = function () {
    if (this == null || this == "") return "";
    var date = new Date(this);
    return date.format("yyyy-MM-dd");
}

String.prototype.toyyyyMMddhhmmss = function () {
    if (this == null || this == "") return "";
    var date = new Date(this);
    return date.format("yyyy-MM-dd hh:mm:ss");
}


//获取QueryString的数组

function getQueryString() {
    var result = location.search.match(new RegExp("[\?\&][^\?\&]+=[^\?\&]+", "g"));
    if (result == null) {
        return "";
    }
    for (var i = 0; i < result.length; i++) {
        result[i] = result[i].substring(1);
    }
    return result;
}

//根据QueryString参数名称获取值
function getQueryStringByName(name) {
    var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i"));
    if (result == null || result.length < 1) {
        return "";
    }
    return result[1];
}

//根据QueryString参数索引获取值

function getQueryStringByIndex(index) {
    if (index == null) {
        return "";
    }
    var queryStringList = getQueryString();
    if (index >= queryStringList.length) {
        return "";
    }
    var result = queryStringList[index];
    var startIndex = result.indexOf("=") + 1;
    result = result.substring(startIndex);
    return result;
}
